package com.zhiyun.yycall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gzit.common.IntentUtils;
import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.zhiyun.message.QueryBalanceRequest;
import com.zhiyun.message.QueryBalanceResponse;
import com.zhiyun.net.AsynCallback;
import com.zhiyun.net.Net;
import com.zhiyun.net.Response;
import com.zhiyun.trace.TraceActivity;
import com.zhiyun.yycall.Config;
import com.zhiyun.yycall.R;

public class AccountActivity extends TraceActivity {

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.fill_btn:
				gotoFill();
				break;
			case R.id.record_btn:
				gotoRecord();
				break;

			case R.id.invite_container:
				gotoInviteActivity();

				break;
			}

		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.account_activity);

		for (int rid : new int[] { R.id.fill_btn, R.id.record_btn,
				R.id.invite_container }) {
			this.findViewById(rid).setOnClickListener(listener);

		}

		((TextView) this.findViewById(R.id.account)).setText(Config
				.getPhoneNo());
	}

	@Override
	protected void onResume() {
		super.onResume();
		remoteQueryBalance();
	}

	private void remoteQueryBalance() {
		String phoneNo = Config.getPhoneNo();
		String password = Config.getPassword();
		if (StringUtils.isBlank(phoneNo) || StringUtils.isBlank(password)) {
			return;
		}

		QueryBalanceRequest req = new QueryBalanceRequest();
		req.setPassword(password);
		req.setPhoneNo(phoneNo);

		Net.doSimpleHttpRemoter(req, new QueryBalanceResponse(),
				new AsynCallback<Response>() {

					@Override
					public boolean callback(Response r) {
						if (r.isFail()) {
							AndroidUtils.toastShort("无法更新账户信息");
							return true;
						}
						QueryBalanceResponse resp = (QueryBalanceResponse) r;
						String balance = resp.getAcctBalance();
						String showBalance = "";
						try {
							float fb = Float.parseFloat(balance);
							showBalance = String.valueOf(fb / 100.0f);
						} catch (Exception e) {
							showBalance = "暂时无法获取余额";

						}

						((TextView) findViewById(R.id.balance))
								.setText(showBalance + "元");
						return false;
					}
				});

	}

	private void gotoInviteActivity() {

		Intent intent = new Intent();
		intent.setClass(this, InviteActivity.class);
		this.startActivity(intent);

	}

	private void gotoFill() {

		IntentUtils.openURL("http://www.baidu.com");

	}

	private void gotoRecord() {

		IntentUtils.openURL("http://www.baidu.com");

	}

}
